package com.jxm.spring.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxm.spring.security.bean.DmAllplatOrderItemCatnameCountDay;
import com.jxm.spring.security.mapper.DmAllplatOrderItemCatnameCountDayMapper;
import com.jxm.spring.security.service.IDmAllplatOrderItemCatnameCountDayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.PipedReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * <p>
 * 每日商品类目销售表 服务实现类
 * </p>
 *
 * @author jxm
 * @since 2021-06-20
 */
@Service
public class DmAllplatOrderItemCatnameCountDayServiceImpl extends ServiceImpl<DmAllplatOrderItemCatnameCountDayMapper, DmAllplatOrderItemCatnameCountDay> implements IDmAllplatOrderItemCatnameCountDayService {

    @Autowired
    private DmAllplatOrderItemCatnameCountDayMapper mapper;

    @Autowired
    @Qualifier(value = "crawlExecutorPool")
    private ExecutorService pool;


//    @Override
//    public List<DmAllplatOrderItemCatnameCountDay> getList() {

//        int listSize = 100;
//        int runSize = 20;
//        int count = listSize / runSize;
//        // 创建一个线程池，数量和开启线程的数量一样
//        ExecutorService executor = Executors.newFixedThreadPool(runSize);
//        System.out.println("开始查询");
//        List<DmAllplatOrderItemCatnameCountDay> result = null;
//        for (int i = 0; i < runSize; i++) {
//
//            int index = i * count;
//
//            int num = count;
//
//            executor.execute(new Runnable() {
//
//                @Override
//
//                public void run() {
//
//                    try {
//
//                        //查询的结果如何保存下来，会不会存在覆盖的问题
//
//                        List<DmAllplatOrderItemCatnameCountDay> result =
//                                this.list();
//
//
//                        //这里做成写入文件的方法
//
//                    } catch (Exception e) {
//
//                        System.out.println("查询失败" + e);
//
//                    }
//
//                }
//
//            });
//
//        }
//
//        // 执行完关闭线程池
//        executor.shutdown();
//
//        return result;
//    }

    @Override
    public List<DmAllplatOrderItemCatnameCountDay> getList2() throws Exception {

        ExecutorService executorService = pool;

        List<DmAllplatOrderItemCatnameCountDay> result = new ArrayList<>();//返回结果

        List<Callable<List<DmAllplatOrderItemCatnameCountDay>>> tasks = new ArrayList<>();

        Callable<List<DmAllplatOrderItemCatnameCountDay>> blockZidongzhan = getBlockZidongzhan();
        tasks.add(blockZidongzhan);
        //Future用于获取结果
        List<Future<List<DmAllplatOrderItemCatnameCountDay>>> futures = executorService.invokeAll(tasks);
        //处理线程返回结果
        if(futures!=null&&futures.size()>0){
            for (Future<List<DmAllplatOrderItemCatnameCountDay>> future:futures){
                result.addAll(future.get());
            }
        }

        executorService.shutdown();//关闭线程池
        return result;

    }


    private Callable<List<DmAllplatOrderItemCatnameCountDay>> getBlockZidongzhan(){
        Callable<List<DmAllplatOrderItemCatnameCountDay>> callable = new Callable<List<DmAllplatOrderItemCatnameCountDay>>() {
            public List<DmAllplatOrderItemCatnameCountDay> call() throws Exception {
                List<DmAllplatOrderItemCatnameCountDay> list=mapper.selectList(new QueryWrapper<>());
                return list;
            }
        };
        return callable;
    }
}
